<html>
<head><meta charset="utf-8"><title>try and type inference · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html">try and type inference</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="196210275"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196210275" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196210275">(May 04 2020 at 17:03)</a>:</h4>
<p><span class="user-mention" data-user-id="127859">@Taylor Cramer</span> If I'm understanding your concern about type inference correctly, you're saying that it would surprise you if we can get away with not writing type annotations for the carrier (e.g. <code>Result&lt;_, _&gt;</code>) because you expect to need annotations for the error type?</p>



<a name="196210287"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196210287" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196210287">(May 04 2020 at 17:03)</a>:</h4>
<p>(because of double conversion?)</p>



<a name="196210681"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196210681" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196210681">(May 04 2020 at 17:06)</a>:</h4>
<p>Right, I'd expect you'd need at least some annotation to tell it what error type you want</p>



<a name="196211000"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196211000" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196211000">(May 04 2020 at 17:08)</a>:</h4>
<p>So, I don't know how feasible this is, but personally I would love it if we had some general rule for double-type-inference cases, that amounts to "convert as far as possible during the first conversion".</p>



<a name="196211039"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196211039" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196211039">(May 04 2020 at 17:09)</a>:</h4>
<p>As in, <code>try { x? }.context(...)?</code> would simply not need to do any conversion the second time, because it already converted to the type it needed the first time.</p>



<a name="196211247"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196211247" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196211247">(May 04 2020 at 17:11)</a>:</h4>
<p>I'm not quite sure I understand-- I don't see "double conversion" as the problem</p>



<a name="196211263"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196211263" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196211263">(May 04 2020 at 17:11)</a>:</h4>
<p>I'm thinking of code like thsi:</p>



<a name="196211315"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196211315" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196211315">(May 04 2020 at 17:11)</a>:</h4>
<p><code>match (try { x? }) { Ok(_) =&gt; ..., Err(_) =&gt; ... }</code></p>



<a name="196211361"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196211361" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196211361">(May 04 2020 at 17:11)</a>:</h4>
<p>what should the error type there be?</p>



<a name="196211424"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196211424" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196211424">(May 04 2020 at 17:12)</a>:</h4>
<p>it's unknown because the error type gets <code>into</code>'d</p>



<a name="196211475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196211475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196211475">(May 04 2020 at 17:12)</a>:</h4>
<p>For one thing, I can't really imagine a case where I'd want to match <code>Err(_)</code> like that without wanting to capture the <code>Err(e)</code>. (Leaving aside that I'm going to go to great lengths to never manually write that match.)</p>



<a name="196211532"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196211532" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196211532">(May 04 2020 at 17:13)</a>:</h4>
<p>And if I'm capturing the <code>e</code> it's likely to do something with it that will determine its type, such as checking an error code inside it.</p>



<a name="196211654"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196211654" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196211654">(May 04 2020 at 17:14)</a>:</h4>
<p>I think this might actually raise a good general point: there's likely value in people talking about their desired use cases for <code>try</code>, how they see themselves using it, so that we can understand how those use cases would be impacted.</p>



<a name="196211673"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196211673" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196211673">(May 04 2020 at 17:14)</a>:</h4>
<p>I personally know that I have code that wants this:</p>
<div class="codehilite"><pre><span></span><code>let res = try { ... };
match res {
    Ok(()) =&gt; {},
    Err(e) =&gt; error!(&quot;Error: {}&quot;, e),
}
</code></pre></div>



<a name="196211722"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196211722" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196211722">(May 04 2020 at 17:15)</a>:</h4>
<p>That code doesn't provide any guidance as to what the type of <code>e</code> is</p>



<a name="196211742"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196211742" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196211742">(May 04 2020 at 17:15)</a>:</h4>
<p><span class="user-mention" data-user-id="127859">@Taylor Cramer</span> I'm curious, why is that a match rather than a macro of some kind, for instance?</p>



<a name="196211808"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196211808" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196211808">(May 04 2020 at 17:15)</a>:</h4>
<p>I mean, I could also write it <code>e.map_err(|e| error!("Error: {}", e))</code> if I don't care about asserting that the <code>Ok</code> case is <code>()</code></p>



<a name="196211823"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196211823" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196211823">(May 04 2020 at 17:15)</a>:</h4>
<p>does that answer your question?</p>



<a name="196211864"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196211864" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196211864">(May 04 2020 at 17:16)</a>:</h4>
<p>...not really?</p>



<a name="196211913"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196211913" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196211913">(May 04 2020 at 17:16)</a>:</h4>
<p>Can you write it how you'd write that code instead?</p>



<a name="196211925"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196211925" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196211925">(May 04 2020 at 17:16)</a>:</h4>
<p>Because if I had to write that once, let alone twice, I'd be trying very hard to factor it out.</p>



<a name="196211994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196211994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196211994">(May 04 2020 at 17:17)</a>:</h4>
<p>what would you write instead?</p>



<a name="196211995"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196211995" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196211995">(May 04 2020 at 17:17)</a>:</h4>
<p><code>macro_rules! check_eprint { ... } check_eprint!(...)</code>, most likely?</p>



<a name="196212035"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196212035" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196212035">(May 04 2020 at 17:17)</a>:</h4>
<p>what does <code>check_print</code> do internally?</p>



<a name="196212090"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196212090" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196212090">(May 04 2020 at 17:17)</a>:</h4>
<p>(Note: I don't know in practice if I'd put the <code>try</code> inside the <code>check_print</code> or in its argument. Would have to think about that.)</p>



<a name="196212208"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196212208" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196212208">(May 04 2020 at 17:18)</a>:</h4>
<p>to clarify: i'm trying to understand what code inside of <code>check_print</code> changes whether this is an inference issue or not</p>



<a name="196212394"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196212394" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196212394">(May 04 2020 at 17:20)</a>:</h4>
<p>(typing, sorry)</p>



<a name="196212398"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196212398" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196212398">(May 04 2020 at 17:20)</a>:</h4>
<p>(In a meeting)</p>



<a name="196212551"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196212551" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196212551">(May 04 2020 at 17:21)</a>:</h4>
<p>no worries, I have to get to other things soon anyways, but I wanted to offer that as one example of a place where the error type will not be inferred</p>



<a name="196212607"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196212607" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196212607">(May 04 2020 at 17:22)</a>:</h4>
<p>So, for one thing the use case is a little hard for me to understand. Does <code>error!</code> print to stderr and then exit the program? Or what does it do?</p>



<a name="196212684"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196212684" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196212684">(May 04 2020 at 17:23)</a>:</h4>
<p>Because I think I'd normally handle that by propagating the error to the top of the program and handling such errors in main.</p>



<a name="196212704"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196212704" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196212704">(May 04 2020 at 17:23)</a>:</h4>
<p>I think I'm with Taylor, I often match on a Result with some branches handling the Ok and some handling the Err, and putting some extra macro around that seems like overkill.</p>



<a name="196212844"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196212844" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196212844">(May 04 2020 at 17:24)</a>:</h4>
<p>That said, if I needed to write that macro, I'd likely use something like <code>if let Err(e) = expr { let e: anyhow::Error = e; error!("Error: {}", e); }</code>.</p>



<a name="196212864"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196212864" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196212864">(May 04 2020 at 17:25)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> I have an extremely low tolerance for boilerplate.</p>



<a name="196212915"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196212915" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196212915">(May 04 2020 at 17:25)</a>:</h4>
<p>Patterns are a sign that some function or macro or other pattern-extraction is needed.</p>



<a name="196212939"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196212939" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196212939">(May 04 2020 at 17:25)</a>:</h4>
<p>I'm glad that the common case of error handling is reduced to <code>?</code>.</p>



<a name="196213001"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196213001" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196213001">(May 04 2020 at 17:26)</a>:</h4>
<p>And I don't want any other "handle the error if any" pattern to be substantially longer than that.</p>



<a name="196213032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196213032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196213032">(May 04 2020 at 17:26)</a>:</h4>
<p>I would agree with your radical anti-boilerplate platform, but when the answer is "so i'll write a macro_rules" I give great, great pause.</p>



<a name="196213084"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196213084" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196213084">(May 04 2020 at 17:27)</a>:</h4>
<p>I can understand that, and I tend to hesitate before writing non-local macros.</p>



<a name="196213111"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196213111" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196213111">(May 04 2020 at 17:27)</a>:</h4>
<p>It seems to me that the question of a macro is really besides the point</p>



<a name="196213135"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196213135" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196213135">(May 04 2020 at 17:27)</a>:</h4>
<p>The point is that there is going to be some code that "handles" the exception</p>



<a name="196213148"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196213148" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196213148">(May 04 2020 at 17:27)</a>:</h4>
<p>That's part of why I observed that in the common case I would want to return the error all the way up to <code>main</code>, in which case I just have to write <code>?</code>.</p>



<a name="196213262"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196213262" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196213262">(May 04 2020 at 17:28)</a>:</h4>
<p><span class="user-mention" data-user-id="127859">@Taylor Cramer</span> Could you clarify what <code>error!</code> does in your sample code?</p>



<a name="196213427"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196213427" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196213427">(May 04 2020 at 17:29)</a>:</h4>
<p>But even in the <code>main</code> case, then</p>



<a name="196213441"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196213441" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196213441">(May 04 2020 at 17:30)</a>:</h4>
<p>the point is that it would be required to manage the exception</p>



<a name="196213485"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196213485" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196213485">(May 04 2020 at 17:30)</a>:</h4>
<p>I believe <code>error!</code> though was meant to be the "error-level log" macro</p>



<a name="196213497"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196213497" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196213497">(May 04 2020 at 17:30)</a>:</h4>
<p>i.e., <span class="user-mention silent" data-user-id="127859">Taylor Cramer</span>'s code was meant to "log the error and move on with life"</p>



<a name="196213544"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196213544" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196213544">(May 04 2020 at 17:30)</a>:</h4>
<p><span class="user-mention" data-user-id="224471">@Lokathor</span> It also occurs to me that as long as you call <code>try</code> explicitly, you could then put the rest in a function rather than a macro, since it doesn't have to stop the propagation of <code>?</code>..</p>



<a name="196213595"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196213595" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196213595">(May 04 2020 at 17:31)</a>:</h4>
<p>And a function has the advantage of being able to include an expected type (e.g. <code>anyhow::Result&lt;()&gt;</code>).</p>



<a name="196213749"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196213749" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196213749">(May 04 2020 at 17:32)</a>:</h4>
<p>So a function like <code>fn check_eprint(result: anyhow::Result&lt;()&gt;) { ... }</code> for error reporting would handle the boilerplate removal and the type inference issue.</p>



<a name="196213835"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196213835" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196213835">(May 04 2020 at 17:32)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116009">nikomatsakis</span> <a href="#narrow/stream/213817-t-lang/topic/try.20and.20type.20inference/near/196213441" title="#narrow/stream/213817-t-lang/topic/try.20and.20type.20inference/near/196213441">said</a>:</p>
<blockquote>
<p>the point is that it would be required to manage the exception</p>
</blockquote>
<p>Right, but if it bubbles up to <code>main</code> it'll have a type by then.</p>



<a name="196213850"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196213850" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196213850">(May 04 2020 at 17:33)</a>:</h4>
<p>Which means it doesn't have a type inference issue.</p>



<a name="196214020"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196214020" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196214020">(May 04 2020 at 17:34)</a>:</h4>
<p>No, it won't</p>



<a name="196214089"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196214089" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196214089">(May 04 2020 at 17:35)</a>:</h4>
<p>i.e., if I write </p>
<div class="codehilite"><pre><span></span><code><span class="k">try</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">foo</span><span class="p">()</span><span class="o">?</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>


<p>the result type of that is fully unknown, today, but even if I constrain to <code>Result&lt;_, _&gt;</code>, the error type is unknown. It is <em>independent</em> of <code>foo()</code>'s return type, except in so far as it is "something that the error of <code>foo</code> can be converted into".</p>



<a name="196214172"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196214172" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196214172">(May 04 2020 at 17:35)</a>:</h4>
<p>This I think is <span class="user-mention" data-user-id="127859">@Taylor Cramer</span>'s point, and I think certainly one of the challenges around try blocks. (The other is "chained" errors, which I think you highlighted earlier.)</p>



<a name="196214280"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196214280" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196214280">(May 04 2020 at 17:36)</a>:</h4>
<p>(Though the two are not independent, it's just that "chained" errors -- i.e., trying to apply <code>?</code> to the result of <code>try</code> block -- are going to lead to type inference failures as a consequence of this.)</p>



<a name="196214344"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196214344" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196214344">(May 04 2020 at 17:37)</a>:</h4>
<p>Right, there are a number of things you could do with the error value (including <code>.into()</code>, as <code>?</code> again would do) that wouldn't fully constrain it</p>



<a name="196248090"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196248090" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Asa Zeren <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196248090">(May 04 2020 at 22:33)</a>:</h4>
<p>I'm wondering how common</p>
<div class="codehilite"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">try</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">x</span><span class="o">?</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="k">match</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="nb">Ok</span><span class="p">(())</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">    </span><span class="nb">Err</span><span class="p">(</span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="n">error</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;Error: {}&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">e</span><span class="p">)}</span><span class="w"></span>
</code></pre></div>


<p>would be, as I see the main advantage of <code>try</code> is allowing things like this: </p>
<div class="codehilite"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">try</span><span class="w"> </span><span class="p">{</span><span class="n">x</span><span class="o">?</span><span class="p">};</span><span class="w"></span>
<span class="k">match</span><span class="w"> </span><span class="n">res</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">     </span><span class="nb">Ok</span><span class="p">(())</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">     </span><span class="nb">Err</span><span class="p">(</span><span class="n">MyErrorType</span>::<span class="n">Foo</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="cm">/* handle this locally */</span><span class="p">}</span><span class="w"></span>
<span class="w">     </span><span class="nb">Err</span><span class="p">(</span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nb">Err</span><span class="p">(</span><span class="n">e</span><span class="p">)</span><span class="w"> </span><span class="c1">// Propagate other errors</span>
</code></pre></div>


<p>In that case, the error type would be specified as <code>MyErrorType </code> (unless<code>_::Foo</code> is added.)</p>



<a name="196249284"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196249284" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196249284">(May 04 2020 at 22:47)</a>:</h4>
<p><span class="user-mention" data-user-id="236160">@Asa Zeren</span> i've personally wanted that (casing out a particular error) much less than switching to logging or adding context, or even throwing the error away entirely</p>



<a name="196249687"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196249687" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Asa Zeren <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196249687">(May 04 2020 at 22:52)</a>:</h4>
<p>If that is your use case, would it not be better to have something like:</p>
<div class="codehilite"><pre><span></span><code><span class="k">fn</span> <span class="nf">log_err</span><span class="o">&lt;</span><span class="n">R</span>: <span class="nc">Try</span><span class="p">(</span><span class="n">res</span>:<span class="nc">R</span><span class="p">)</span>-&gt;<span class="nb">Option</span><span class="o">&lt;</span><span class="n">R</span>::<span class="n">Output</span><span class="o">&gt;</span><span class="p">{</span><span class="w"> </span><span class="cm">/* ... */</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="196249701"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/try%20and%20type%20inference/near/196249701" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Asa Zeren <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/try.20and.20type.20inference.html#196249701">(May 04 2020 at 22:52)</a>:</h4>
<p>Though I guess that has the same issue</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>